from pao_python.pao.service.security.security_utility import PermissionUtility
from app.permission import Permission
from service.user_func import User_Server
from service.stencil_management import StencilManageMent

def register(jsonrpc,db_addr,db_name,db_port,session,inital_password):
    stencil_management = StencilManageMent(db_addr,db_port,db_name)
    Permission_utility = PermissionUtility(db_addr,db_port,db_name,session)
    User_func = User_Server(db_addr, db_port,db_name,inital_password,session)
    PERMISSIOM = '有此权限'

    #新增/编辑模版信息
    @jsonrpc.method('IStencilService.insert_stencil')
    def stencil_insert_service(data):
        def pass_permission():
            is_insert = False
            #新建
            if 'id' not in data.keys():            
                user_list = User_func.get_current_user_by_account_id(session['account_id'])
                data['user_id'] = user_list['id']
                is_insert = True
            res = stencil_management.insert_stencil(data,is_insert)
            return res
        return Permission_utility.is_peimission_pass(pass_permission,Permission.Stencil_Select)
    
    #获取模版信息
    @jsonrpc.method('IStencilService.get_stencil')
    def get_stencil_service(stencil_id):
        def pass_permission():
            res = stencil_management.get_stencil(stencil_id)
            return res
        return Permission_utility.is_peimission_pass(pass_permission,Permission.Stencil_Select)
        

    #获取模版列表信息
    @jsonrpc.method('IStencilService.get_stencil_list')
    def get_stencil_list_service(condition,page,size):
        '''模版列表查询'''
        def pass_permission():
            res = stencil_management.get_stencil_list(condition,page,size)
            return res
        if Permission_utility.is_permission(Permission.AllSign_Select) == PERMISSIOM:
            return Permission_utility.is_peimission_pass(pass_permission,Permission.AllSign_Select)
        else:
            user_list = User_func.get_current_user_by_account_id(session['account_id'])
            condition['user_id'] = user_list['id']            
            return Permission_utility.is_peimission_pass(pass_permission,Permission.Stencil_Select)

    #模版删除
    @jsonrpc.method('IStencilService.del_stencil')
    def delete_stencil_service(stencil_ids):
        def pass_permission():
            # user_list = User_func.get_current_user_by_account_id(session['account_id'])
            # res = stencil_management.del_stencil(stencil_id,user_list['id'])
            res = stencil_management.del_stencil(stencil_ids)
            return res
        return Permission_utility.is_peimission_pass(pass_permission,Permission.Stencil_Select)
    